import { createStore } from "vuex"
import subject from './modules/subject'
import {
	wx_customer_data,
	wx_clockin_rules_data
} from '@/api'

const store = createStore({
	modules: {
		subject
	},
	state: {
		statusBarHeight: 0,
		isLogin: false,
		userInfo: {},
		expireCount: 0,
		defaultInfo: {
			name: '',
			avatar: ''
		},
		// 是否冻结
		isFrozen: false,
		isUserInfo: false, // 是否获取微信用户基本信息

		// 打卡设置数据
		clockInRuleData: {}, // 打卡规则
		clockInDetail: {}, // 打卡详情
		topicDetail: null
	},
	mutations: {
		setUserInfo(state, data) {
			if (data) state.userInfo = data
		},
		setExireCount(state, data = 0) {
			state.expireCount = data
		},
		setStatusBarHeight(state, data) {
			state.statusBarHeight = data
		},
		setLoginState(state, data = false) {
			state.isLogin = data
		},
		setFrozenState(state, data = false) {
			state.isFrozen = data
		},
		setClockInSettingData(state, data = null) {
			if (data) {
				for (let key in data) {
					state[key] = data[key]
				}
			}
		},
		setIsUserInfo(state, data = false) {
			state.isUserInfo = data
		},
		setClockInRuleData(state, data) {
			state.clockInRuleData = data
		},
		setClockInDetail(state, data = null) {
			state.clockInDetail = data
		},
		setTopicDetail(state, data = null) {
			state.topicDetail = data
		},
		setAppDefaultInfo(state, data = null) {
			if (data) {
				state.defaultInfo = { ...data }
			}
		}
	},
	actions: {
		getUserInfo({
			commit
		}, data) {
			wx_customer_data().then(res => {
				if (!res.customer_data) {
					commit('setLoginState', false)
				} else {
					commit('setLoginState', true)
					store.dispatch('setUserInfo', res.customer_data)
					if (res.customer_data.avatar) {
						commit('setIsUserInfo', true)
					}
				}
				commit('setExireCount', res.expireCount)
				commit('setFrozenState', !!res.is_frozen)
			})
		},
		setLoginState({
			commit
		}, data) {
			commit('setLoginState', data)
		},
		setUserInfo({
			commit
		}, data) {
			commit('setUserInfo', data)
		},
		setClockInSetting({
			commit
		}, data) {
			commit('setClockInSettingData', data)
		},
		setIsUserInfo({
			commit
		}, data) {
			commit('setIsUserInfo', data)
		},
		async getClockInRuleData({
			commit
		}, data) {
			wx_clockin_rules_data().then(res => {
				commit('setClockInRuleData', res.rule_data || {})
			})
		},
		setClockInDetail({
			commit
		}, data) {
			commit('setClockInDetail', data)
		},
		setTopicDetail({
			commit
		}, data) {
			commit('setTopicDetail', data)
		},
		setAppDefaultInfo({
			commit
		}, data) {
			commit('setAppDefaultInfo', data)
		},
	},
	getters: {}
})

export default store